package cn.kduck.plugin.idea.dialog;

import javax.swing.*;
import java.awt.*;


public class MapBeanForm {
    private JPanel mainPanel;
    private JComboBox genTypeComboBox;
    private JCheckBox genNewClassCheckBox;
    private JTextField classNameTextField;
    private JList propertiesList;

    private DefaultListModel listModel = new DefaultListModel();
    private DefaultComboBoxModel<NameValuePair> comboBoxModel = new DefaultComboBoxModel();

    private PropertiesSelectionListener listener;

    public MapBeanForm(String className) {
        comboBoxModel.addElement(new NameValuePair("常量模式","CONSTANT"));
        comboBoxModel.addElement(new NameValuePair("枚举模式","ENUM"));

        propertiesList.setModel(listModel);
        genTypeComboBox.setModel(comboBoxModel);

        classNameTextField.setText(className);
        propertiesList.setCellRenderer(new CustomListCellRenderer());
        genNewClassCheckBox.addChangeListener(e -> {
            boolean newClass = genNewClassCheckBox.isSelected();
            classNameTextField.setEditable(newClass);
            if(newClass){
                classNameTextField.setText(className + "MapBean");
            }else{
                classNameTextField.setText(className);
            }
        });
        propertiesList.addListSelectionListener(e ->{
            int[] selectedIndices = propertiesList.getSelectedIndices();
            if(listener != null){
                listener.onSelectChanged(selectedIndices);
            }
        });
    }

    protected void addPropertiesSelectionListener(PropertiesSelectionListener listener) {
        this.listener = listener;
    }

    private class CustomListCellRenderer extends DefaultListCellRenderer {

        @Override
        public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            Component component = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

            String[] items = value.toString().split(":");
            if (items.length <= 1 || !("String".equals(items[1])
                    || "Integer".equals(items[1])
                    || "Date".equals(items[1])
                    || "Long".equals(items[1])
                    || "Double".equals(items[1])
                    || "Boolean".equals(items[1])
                    || "int".equals(items[1])
                    || "long".equals(items[1])
                    || "boolean".equals(items[1])
                    || "double".equals(items[1]))) {
                component.setForeground(new Color(255, 99,33));
            }

            return component;
        }
    }

    public void addField(String fieldName){
        listModel.addElement(fieldName);
    }

    public JPanel getMainPanel() {
        return mainPanel;
    }

    public boolean isNewClass() {
        return genNewClassCheckBox.isSelected();
    }

    public void selectAll(){
        int itemCount = listModel.getSize();
        int[] indices = new int[itemCount];
        for (int i = 0; i < itemCount; i++) {
            indices[i] = i;
        }
        propertiesList.setSelectedIndices(indices);
    }

    public String getType(){
        return comboBoxModel.getElementAt(genTypeComboBox.getSelectedIndex()).getValue();
    }

    public int[] getSelectedIndices(){
        return propertiesList.getSelectedIndices();
    }

    interface PropertiesSelectionListener {
        void onSelectChanged(int[] selectedIndices);
    }


    private class NameValuePair {
        private String name;
        private String value;

        public NameValuePair() {
        }

        public NameValuePair(String name, String value) {
            this.name = name;
            this.value = value;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }

        @Override
        public String toString() {
            return getName();
        }
    }

}
